package com.cskaoyan.service;

import com.cskaoyan.bean.Collect;
import com.cskaoyan.bean.CollectExample;
import com.cskaoyan.bean.User;
import com.cskaoyan.bean.wxcollect.AddOrDeleteData;
import com.cskaoyan.bean.wxcollect.AddOrDeleteReq;
import com.cskaoyan.bean.wxcollect.CollectListBean;
import com.cskaoyan.bean.wxcollect.WxCollectData;
import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: yudefu
 * @date 2021年06月07日  17:52
 * @description: 收藏模块
 */

@Service
public class WxCollectServiceImpl implements WxCollectService {

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    GoodsMapper goodsMapper;


    @Override
    public WxCollectData getCollectList(Byte preType, Integer page, Integer size) {

        PageHelper.startPage(page, size);

        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andTypeEqualTo(preType);
        criteria.andDeletedEqualTo(false);

        List<Collect> collects = collectMapper.selectByExample(collectExample);
        int total = collects.size();
        int totalPages = total / size + 1;

        List<CollectListBean> collectListBeans = new ArrayList<>();
        for (Collect collect : collects) {
            Integer valueId = collect.getValueId();

            CollectListBean collectListBean = goodsMapper.selectByGoodsId(valueId);
            collectListBean.setId(collect.getId());
            collectListBean.setType(collect.getType());
            collectListBean.setValueId(collect.getValueId());

            collectListBeans.add(0,collectListBean);

        }

        WxCollectData wxCollectData = new WxCollectData();
        wxCollectData.setCollectList(collectListBeans);
        wxCollectData.setTotalPages(totalPages);

        return wxCollectData;
    }

    @Override
    public AddOrDeleteData addOrDelete(AddOrDeleteReq addOrDeleteReq) {

        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andTypeEqualTo(addOrDeleteReq.getType());
        criteria.andValueIdEqualTo(addOrDeleteReq.getValueId());
        criteria.andDeletedEqualTo(false);

        List<Collect> collects = collectMapper.selectByExample(collectExample);

        if (collects.size() != 0) {

            Collect collect = collects.get(0);
            collect.setDeleted(true);
            collect.setUpdateTime(new Date());

//            CollectExample collectExampleUpdate = new CollectExample();
//            CollectExample.Criteria criteriaUpdate = collectExampleUpdate.createCriteria();
//            criteriaUpdate.andIdEqualTo(collect.getId());
            Integer affectRows = collectMapper.updateByPrimaryKey(collect);
//            int i = 1/0;

//            Integer affectRows = collectMapper.updateById(collect);
            AddOrDeleteData deleteData = new AddOrDeleteData();
            deleteData.setType(0);
            return deleteData;
        }

        User user = (User) SecurityUtils.getSubject().getPrincipal();

        if (user == null) {
            return null;
        }

        Collect addCollect = new Collect();
        addCollect.setId(0);
        addCollect.setUserId(user.getId());
        addCollect.setValueId(addOrDeleteReq.getValueId());
        addCollect.setType(addOrDeleteReq.getType());
        addCollect.setAddTime(new Date());
        addCollect.setUpdateTime(new Date());
        addCollect.setDeleted(false);

        int insert = collectMapper.insert(addCollect);

        AddOrDeleteData addData = new AddOrDeleteData();
        addData.setType(1);

        return addData;
    }
}

























